local math = _G.math
local RandomLogic = require("common.RandomLogic")
local EquipExcel = require("excel.Equip")
local EquipConf = EquipExcel.equip
local EquipScore = EquipExcel.equipScore
local EquipQulityConf = EquipExcel.equipQulity
local StrengthConfig = require("excel.Strength")
local GemExcel = require("excel.Gem")
local GemConf = GemExcel.gem
local RoleDefine = require("role.RoleDefine")
local ItemDefine = require("bag.ItemDefine")
local StrenthAllConfig = StrengthConfig.strengthAll

function  copy(dst, src)
    dst.quality = src.quality
    dst.hole = src.hole
    dst.strengthLv = src.strengthLv or 0
    if dst.gems == nil then
        dst.gems = {}
    end
    for i = 0,dst.hole-1 do
        dst.gems[i] = src.gems[i] or 0
    end
    --锻造
    dst.strengthLv = src.strengthLv
    if not dst.xiLianStar then
        dst.xiLianStar = {}
        dst.xiLianAttr = {}
        dst.xiLianAttrVal = {}
    end
    for i = 0, 3 do
        dst.xiLianStar[i] = src.xiLianStar[i];
        dst.xiLianAttr[i] = src.xiLianAttr[i];
        dst.xiLianAttrVal[i] = src.xiLianAttrVal[i];
    end
    dst.xiLianPower = src.xiLianPower
    dst.xiLianTimes = src.xiLianTimes
    dst.power = src.power or 0
end

function create(equip,id,qualityOut, strengthLv, grid)

    if qualityOut and type(qualityOut) ~= "table" and type(qualityOut) ~= "number" then
        assert()
    end
    -- 得到品质
    local quality = EquipConf[id].quality
    if quality == 0 then
        if type(qualityOut) == "number" and qualityOut ~= 0 then
            quality = qualityOut
        else
            if qualityOut == 0 then
                qualityOut = nil
            end
            quality = RandomLogic.getRandom2(EquipQulityConf,qualityOut)
            if not quality then
                quality = 1
            end
        end
    end
    --品质，强化，洗练
    equip.quality = quality
    equip.strengthLv = strengthLv or 0
    if not equip.xiLianStar then
      equip.xiLianStar = {}
    end
    if not equip.xiLianAttr then
      equip.xiLianAttr = {}
    end
    if not equip.xiLianAttrVal then 
      equip.xiLianAttrVal = {}
    end
    for i = 0, 3 do
        equip.xiLianStar[i] = 0
        equip.xiLianAttr[i] = 0
        equip.xiLianAttrVal[i] = 0
    end
    equip.xiLianTimes = 0
    equip.xiLianPower = 0
    --宝石孔
    equip.hole = ItemDefine.getRealQuality(equip.quality) - 1    
    
    if not equip.gems then
      equip.gems = {}
    end

    for i = 0, 4 do
        equip.gems[i] = 0
    end
    if EquipConf[id].dressType ~= 1 then
      equip.hole = 0
      if not equip.gems then
        equip.gems = {}
      end
    end
    equip.power = getZhandouli(nil, grid)
    return equip
end

function getValue(key,grid)
    if EquipConf[grid.id] == nil then
        return 0
    end
    local equip = grid.data.equip
    local value = 0
    for k,v in ipairs(EquipConf[grid.id].baseAttr) do
        --基础
        if key == v[1] then
          value = value + getBaseAttrValue(v[2], equip.quality, grid.id)
        end
    end

    --强化
    local strengthenID = EquipConf[grid.id].strengthenID
    local equipStrengthenConf = StrengthConfig[strengthenID]
    local strengthenLv = grid.data.equip.strengthLv or 0
    if strengthenLv ~= 0 then
        local addAttr = equipStrengthenConf[strengthenLv].addAttrs
        for i=1, #addAttr do
          if key == addAttr[i][1] then
            value = value + addAttr[i][2]
          end
        end
    end

    -- gem
    for i=0, equip.hole - 1 do
        local gemConf = GemConf[equip.gems[i]]
        if equip.gems[i] ~= 0 then
            for __,v in pairs(gemConf.attr) do
              if v[1] == key then
                value = value + v[2]
              end
            end
        end
    end

    --洗练
    for i=0, 3 do
        if equip.xiLianAttr[i] ~= 0 then
            if key == equip.xiLianAttr[i] then
              value = value + equip.xiLianAttrVal[i]
            end
        end
    end
    return value
end

function getPutOnLv(grid)
    local lv = ItemDefine.getValue(grid.id, "lv")
    return lv or 1
end



function getColor(quality)
    return ItemDefine.getRealQuality(quality)
end



-- 取战斗力
function getZhandouli(human, grid)
    local zhandouli = 0
    if not grid then
        return zhandouli
    end
    local equip = grid.data.equip
    -- 基础属性
    for k,v in ipairs(EquipConf[grid.id].baseAttr) do
        --基础 
        zhandouli = zhandouli + EquipScore[v[1]].score * getBaseAttrValue(v[2], equip.quality, grid.id)
    end

    --强化
    local strengthenID = EquipConf[grid.id].strengthenID
    local equipStrengthenConf = StrengthConfig[strengthenID]
    local strengthenLv = grid.data.equip.strengthLv or 0
    if strengthenLv ~= 0 then
        local addAttr = equipStrengthenConf[strengthenLv].addAttrs
        if not addAttr then
            return 0
        end
        for i=1, #addAttr do
            zhandouli = zhandouli + EquipScore[ addAttr[i][1]].score * addAttr[i][2]
        end
    end

    -- gem
    for i=0, equip.hole - 1 do        
        if equip.gems[i] and equip.gems[i] ~= 0 then
            local gemConf = GemConf[equip.gems[i]]
            for key,value in pairs(gemConf.attr) do
                zhandouli = zhandouli + EquipScore[value[1]].score * value[2]
            end
        end
    end

    --洗练
    for i=0, 3 do
        if equip.xiLianAttr[i] ~= 0 then
            zhandouli = zhandouli + EquipScore[equip.xiLianAttr[i]].score * equip.xiLianAttrVal[i]
        end
    end
    equip.power = math.floor(zhandouli)
    return zhandouli
end

--默认基础属性 × ( 1 + 品质加成百分比  ) baseAttrAdd 0.1
function getBaseAttrValue(value,quality, id)
    if EquipConf[id].dressType ~= 1 then
      return value
    end
    return value *  ( 1 + EquipQulityConf[quality].attrAddRate)
end

if not _G.is_world then


    PURIFY_MAX_CNT = 15

    attrKey2Str = attrKey2Str or {}
    function init()
        for k,v in pairs(RoleDefine) do
            if type(v) == "number" then
                attrKey2Str[v] = string.lower(k)
            end
        end
    end

--    local function getSkillID(id, vipStarCnt, key)
--        local equipConf = EquipConf[id]
--        if not equipConf then
--            assert()
--        end
--        local vipAttrID = equipConf.vipAttrID
--        if EquipVipAttrConf.vipAttr[vipAttrID] == nil then
--            assert(nil, "equipvipattr has no config:" .. vipAttrID .. " equipID:" .. id)
--        end
--
--        local vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr0
--        if vipStarCnt == 5 then vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr5
--        elseif vipStarCnt == 4 then vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr4
--        elseif vipStarCnt == 3 then vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr3
--        elseif vipStarCnt == 2 then vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr2
--        elseif vipStarCnt == 1 then vipAttrConf = EquipVipAttrConf.vipAttr[vipAttrID].vipAttr1
--        end
--
--        for k,v in ipairs(vipAttrConf) do
--            if v[1] == key then
--                return v[4]
--            end
--        end
--    end

    function calcAttr(human,grid)
        local equip = grid.data.equip
        local quality =  equip.quality

        -- 基础属性
        for k,v in ipairs(EquipConf[grid.id].baseAttr) do
            human.attr[v[1]] = human.attr[v[1]] or 0
            human.attr[v[1]] = human.attr[v[1]] + getBaseAttrValue(v[2],quality,grid.id)
        end

        --强化属性
        getStrengthen(human, grid)

        --镶嵌属性
        for i = 0, equip.hole-1 do
            if equip.gems[i] ~= 0 then
                local gemAttr = GemConf[equip.gems[i]]
                for key,value in ipairs(gemAttr.attr) do
                    human.attr[value[1]] = (human.attr[value[1]] or 0) + value[2]
                end
            end
        end

        for i = 0, 3 do
            if equip.xiLianAttr[i] and equip.xiLianAttr[i] ~= 0 then
                human.attr[equip.xiLianAttr[i]] = human.attr[equip.xiLianAttr[i]] + equip.xiLianAttrVal[i]
            end
        end
    end

    function doCalc(human)
        -- 先计算累加值，再计算百分比
        local minStrengthLv = -1
        for i = 0, human.db.equipLen-1 do
            local grid = human.db.equip[i]
            if grid ~= nil and grid.id ~=0 then
                if  grid.lifeTime ==0 or grid.lifeTime > os.time() then
                    calcAttr(human,grid)
                end
                if minStrengthLv == -1 or grid.data.equip.strengthLv < minStrengthLv then
                    minStrengthLv = grid.data.equip.strengthLv
                end
            end
        end
        --全身强化属性
        for i=#StrenthAllConfig, 1, -1 do
          if minStrengthLv >= StrenthAllConfig[i].strengthLv then
              for j=1,#StrenthAllConfig[i].addAttr do
                  human.attr[StrenthAllConfig[i].addAttr[j][1]] = human.attr[StrenthAllConfig[i].addAttr[j][1]] + StrenthAllConfig[i].addAttr[j][2]
              end              
              break
          end
        end
        
        --全身宝石属性+%d
        local ALL_GEM_ATTR = human.attr[RoleDefine.ALL_GEM_ATTR] or 0       
        if ALL_GEM_ATTR > 0 then 
          for i = 0, human.db.equipLen-1 do
            local grid = human.db.equip[i]
            if grid ~= nil and grid.id ~=0 then
                if  grid.lifeTime ==0 or grid.lifeTime > os.time() then
                    local equip = grid.data.equip
                    for j = 0, equip.hole - 1 do
                        if equip.gems[j] ~= 0 then
                           local gemAttr = GemConf[equip.gems[j]]
                           for key,value in ipairs(gemAttr.attr) do
                              human.attr[value[1]] = (human.attr[value[1]] or 0) + value[2]*ALL_GEM_ATTR
                           end
                        end
                    end
                end
            end
          end
        end
    end

    --得到强化属性
    function getStrengthen(human, grid)
        local strengthenID = EquipConf[grid.id].strengthenID
        local equipStrengthenConf = StrengthConfig[strengthenID]
        local strengthenLv = grid.data.equip.strengthLv or 0
        if strengthenLv == 0 then return end
        local addAttr = equipStrengthenConf[strengthenLv].addAttrs
        if not addAttr then
            return
        end
        for i=1, #addAttr do
            human.attr[addAttr[i][1]] = human.attr[addAttr[i][1]] or 0
            human.attr[addAttr[i][1]] = human.attr[addAttr[i][1]] + addAttr[i][2]
        end
    end

--    local function isKeyAddRate(key) -- 属性是否是加百分比的
--        if RoleDefine.KEY_ADD_RATE[key] then return true end
--    end

    -- 杀怪经验值 脱装备或者装备洗炼时需要置：nil －－  human.killMonsterExpAddValue
    function getKillMonsterExpAddValue(human)
--        if human.killMonsterExpAddValue then
--        		return human.killMonsterExpAddValue
--        end
        local killMonsterExpUp = 0
        for i=0, human.db.equipLen - 1 do
            local grid = human.db.equip[i]
            if grid.id ~= 0 then
              if grid.lifeTime ~= 0 then
                if grid.lifeTime > os.time() then
                    killMonsterExpUp = killMonsterExpUp + getValue(RoleDefine.KILL_MONSTER_EXP_ADD,grid)
                end
              else
                killMonsterExpUp = killMonsterExpUp + getValue(RoleDefine.KILL_MONSTER_EXP_ADD,grid)
              end
            end
        end
        human.killMonsterExpAddValue = killMonsterExpUp
        return human.killMonsterExpAddValue
    end
    -- 杀怪经验比率 脱装备或者装备洗炼时需要置：nil －－  human.killMonsterExpAddRate
    function getKillMonsterExpAddRate(human)

--        if human.killMonsterExpAddRate then
--        		return human.killMonsterExpAddRate
--        end

        local killMonsterExpUp = 0
        for i=0, human.db.equipLen - 1 do
            local grid = human.db.equip[i]
            if grid.id ~= 0 then
              if grid.lifeTime ~= 0 then
                if grid.lifeTime > os.time() then
                    killMonsterExpUp = killMonsterExpUp + getValue(RoleDefine.KILL_MONSTER_EXP_ADD_RATE, grid)
                end
              else
                killMonsterExpUp = killMonsterExpUp + getValue(RoleDefine.KILL_MONSTER_EXP_ADD_RATE, grid)
              end
            end
        end
        human.killMonsterExpAddRate = killMonsterExpUp
        return human.killMonsterExpAddRate

    end
    --是否是右边的装备
    function isRightEquip(id)
      return EquipConf[id].pos == 3 or EquipConf[id].pos == 5 or EquipConf[id].pos == 7 or EquipConf[id].pos == 9 or EquipConf[id].pos == 11
    end
    function getEquipLvCnt(human, lv, needCnt)
      local cnt = 0
      for i = 0, human.db.equipLen-1 do
        local grid = human.db.equip[i]
        if grid ~= nil and grid.id ~=0 then
          if EquipConf[grid.id].lv >= lv then
            if needCnt == 5 and isRightEquip(grid.id) then
              cnt = cnt + 1
            elseif needCnt ~= 5 then
              cnt = cnt + 1
            end
          end 
        end
      end
      return cnt
    end
    function getEquipQualityCnt(human, quality, needCnt)
      local cnt = 0
      for i = 0, human.db.equipLen-1 do
        local grid = human.db.equip[i]
        if grid ~= nil and grid.id ~=0 then
          if grid.data.equip.quality >= quality then
            if needCnt == 5 and isRightEquip(grid.id) then
              cnt = cnt + 1
            elseif needCnt ~= 5 then
              cnt = cnt + 1
            end
          end 
        end
      end
      return cnt
    end
    init()

end
